package com.zjjt.dc.modules.srv.service.lw.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjjt.dc.common.query.QueryGenerator;
import com.zjjt.dc.modules.datacatch.entity.lw.DcCatchDataLwCustomerHoldings;
import com.zjjt.dc.modules.datacatch.entity.lw.DcCatchDataLwPriceDr;
import com.zjjt.dc.modules.datacatch.entity.lw.DcCatchDataLwWh;
import com.zjjt.dc.modules.datacatch.service.IDcCatchDataLwCustomerHoldingsService;
import com.zjjt.dc.modules.datacatch.service.lw.IDcDataCatchLwWhService;
import com.zjjt.dc.modules.datacatch.vo.lw.DcCatchDataLwPriceVO;
import com.zjjt.dc.modules.srv.entity.DcCatchDataLwPriceCheck;
import com.zjjt.dc.modules.srv.mapper.IDcCatchDataLwPriceCheckMapper;
import com.zjjt.dc.modules.srv.service.lw.IDcCatchDataLwPriceCheckService;
import com.zjjt.dc.modules.srv.vo.DcCatchDataLwPriceCheckVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class DcCatchDataLwPriceCheckServiceImpl extends ServiceImpl<IDcCatchDataLwPriceCheckMapper, DcCatchDataLwPriceCheck> implements IDcCatchDataLwPriceCheckService {

    @Resource
    private IDcDataCatchLwWhService whService;
    @Resource
    private IDcCatchDataLwCustomerHoldingsService holdingsService;
    @Override
    public List<DcCatchDataLwPriceCheck> getCreateById(String id) {
        DcCatchDataLwPriceCheck entity = new DcCatchDataLwPriceCheck();
        entity.setCreateBy(Long.parseLong(id));
        QueryWrapper<DcCatchDataLwPriceCheck> queryWrapper = QueryGenerator.initQueryWrapper(new DcCatchDataLwPriceCheck(), null);
        queryWrapper.eq("create_by", id);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<DcCatchDataLwPriceCheckVO> getDcCatchDataLwPriceVO(List<DcCatchDataLwPriceCheck> list) {
        //获取未处理的数据
        List<DcCatchDataLwWh> unProcess = whService.getUnProcess();
        List<DcCatchDataLwPriceCheckVO> listVO = new ArrayList<>();

        list.forEach(item -> {
            unProcess.stream().filter(f->f.getExUserId().equals(item.getExUserId())&&f.getBreedId().equals(item.getProductCode())).forEach(e->{
                DcCatchDataLwPriceCheckVO dcCatchDataLwPriceVO = JSONObject.parseObject(JSONObject.toJSONString(e), DcCatchDataLwPriceCheckVO.class);
                dcCatchDataLwPriceVO.setAmount(item.getAmount());
                //获取价格
                DcCatchDataLwCustomerHoldings one = holdingsService.getOne(new QueryWrapper<DcCatchDataLwCustomerHoldings>()
                        .eq("product_code", e.getBindId())
                        .le("create_time", e.getExPassTime() == null ? e.getExCreateTime() : e.getExPassTime()).last("limit 1"), false);
                if (one!= null){
                    dcCatchDataLwPriceVO.setAmount(one.getAvgInventoryPrice());
                }

                listVO.add(dcCatchDataLwPriceVO);
            });

        });
        return listVO;
    }

}